package top.ahaoao.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ahaoao.pojo.Account;
import top.ahaoao.mapper.AccountMapper;
import top.ahaoao.service.AccountService;

import java.util.List;

/**
 * @author ：ahao
 * @version : 1.0
 * @date ：Created in 2020/11/12 16:41
 * @description ：账户服务接口实现类
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public List<Account> findAll() {
        return accountMapper.findAll();
    }

    @Override
    public Account findByName(String name) {
        return accountMapper.findByName(name);
    }

    @Override
    public Account findById(Integer id) {
        return accountMapper.findById(id);
    }

    @Override
    public void updateAccount(Account account) {
        accountMapper.updateAccount(account);
    }

    @Override
    public void saveAccount(Account account) {
        accountMapper.saveAccount(account);
    }

    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        Account sourceAccount = accountMapper.findByName(sourceName);
        Account targetAccount = accountMapper.findByName(targetName);

        sourceAccount.setMoney(sourceAccount.getMoney()-money);
        targetAccount.setMoney(targetAccount.getMoney()+money);

        accountMapper.updateAccount(sourceAccount);
//        int a = 1/0;  //测试springAOP事务控制
        accountMapper.updateAccount(targetAccount);
    }
}

